/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/memory
// Copyright( c) 2013.  All Rights Reserved
//
// File:		AEMallocAllocator.cpp
// Author:		Gianluca Belardelli
// Date:		31/12/2013
//
/////////////////////////////////////////////////////////////////////////
#include <AECore.h>
#include <core\memory\AEMallocAllocator.h>

bool g_bFirstUse = true;

AEMallocAllocator::~AEMallocAllocator( void )
{
}

void *AEMallocAllocator::BlockAlloc( AEUINT32 uiNumBytes, AEMemoryAlign maAlign )
{
	if( g_bFirstUse )
	{
		m_msStats.uiAllocated = 0;
		m_msStats.uiAvailable = 0xFFFFFFFF;
		m_msStats.uiInUse = 0;
		m_msStats.uiLargestBlock = 0;
		m_msStats.uiPeakInUse = 0;
		m_msStats.uiTotalAllocated = 0;
		g_bFirstUse = false;
	}

	void *lpPtr = (void *)malloc( uiNumBytes );
	if( lpPtr != 0L )
	{
		m_msStats.uiAllocated+=uiNumBytes;
		m_msStats.uiInUse+=uiNumBytes;
		m_msStats.uiTotalAllocated++;

		if( m_msStats.uiLargestBlock < uiNumBytes )
			m_msStats.uiLargestBlock = uiNumBytes;

		return lpPtr;
	}

	return 0L;
}

void AEMallocAllocator::BlockFree( void *lpPtr, AEUINT32 uiNumBytes )
{
	m_msStats.uiAllocated-=uiNumBytes;
	m_msStats.uiInUse-=uiNumBytes;
	//m_msStats.uiTotalAvailable--;
	free( lpPtr );
}

void AEMallocAllocator::GetMemoryStats( AEMemStats &msMemStats )
{
	memcpy( &msMemStats, &m_msStats, sizeof( AEMemStats ) );
}

AEINT32 AEMallocAllocator::GetAllocatedSize( const void *lpObj, AEINT32 nBytes )
{
	return 1;
}
